package com.tanhua.dubbo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.Question;
import com.tanhua.dubbo.QuestionAPI;
import com.tanhua.dubbo.mapper.QuestionMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class QuestionAPIImpl implements QuestionAPI {

    @Autowired
    private QuestionMapper questionMapper;


    @Override
    public Question findByUserId(Long userId) {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return questionMapper.selectOne(wrapper);
    }

    @Override
    public void saveOrUpdate(Long userId, String content) {
        //1 根据userId查找question
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Question question = questionMapper.selectOne(wrapper);
        //2 找到--执行更新操作
        if (question!=null){
            question.setTxt(content);
            questionMapper.updateById(question);
        }else {
            //3 找不到--执行插入操作
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionMapper.insert(question);
        }


    }
}
